package grouuu;

import grouuu.data.Context;
import grouuu.layer.LayerGround;
import openfl.geom.Point;

/**
 * ...
 * @author Grouuu
 */
class World
{
	static private inline var SPEED:Int = 200; // pixels/s
	
	private var context:Context;
	private var layerGround:LayerGround;
	
	private var originX:Float;
	private var originY:Float;
	
	public var targetPositionX:Float;
	public var targetPositionY:Float;

	public function new(context:Context) 
	{
		this.context = context;
	}
	
	public function init():Void
	{
		initLayers();
		
		originX = context.tileW;
		originY = context.tileH;
		
		targetPositionX = 0;
		targetPositionY = 0;
	}
	
	private function initLayers():Void
	{
		layerGround = new LayerGround(context);
		
		context.scene.addChild(layerGround);
	}
	
	public function update(deltaTime:Int):Void
	{
		var delta:Float = (SPEED * deltaTime) / 1000;
		
		if (context.controllerType == Context.CONTROLLER_KEY)
		{
			if (context.key.LEFT)
				originX -= delta;
			else if (context.key.RIGHT)
				originX += delta;
			
			if (context.key.UP)
				originY -= delta;
			else if (context.key.DOWN)
				originY += delta;
		}
		else if (context.controllerType == Context.CONTROLLER_MOUSE)
		{
			//trace(targetPositionX, targetPositionY);
			
			if (targetPositionX != 0)
				trace(context.tiles.getPath(new Point(context.stageW / 2, context.stageH / 2), new Point(300, 300), new Point(originX, originY)));
			
			/*if (targetPositionX != 0)
			{
				if (delta < (targetPositionX < 0 ? -targetPositionX : targetPositionX)) // Math.abs(targetPositionX)
				{
					if (targetPositionX > 0)
					{
						targetPositionX -= delta;
						originX += delta;
					}
					else
					{
						targetPositionX += delta;
						originX -= delta;
					}
				}
				else
				{
					if (targetPositionX > 0)
					{
						originX += targetPositionX;
					}
					else
					{
						originX -= targetPositionX;
					}
					
					targetPositionX = 0;
				}
			}
			
			if (targetPositionY != 0)
			{
				if (delta < (targetPositionY < 0 ? -targetPositionY : targetPositionY)) // Math.abs(targetPositionX)
				{
					if (targetPositionY > 0)
					{
						targetPositionY -= delta;
						originY += delta;
					}
					else
					{
						targetPositionY += delta;
						originY -= delta;
					}
				}
				else
				{
					if (targetPositionY > 0)
					{
						targetPositionY += targetPositionY;
					}
					else
					{
						targetPositionY -= targetPositionY;
					}
					
					targetPositionY = 0;
				}
			}*/
			
			/*if (targetPositionX > 0)
			{
				targetPositionX -= (SPEED * deltaTime) / 1000;
				
				if (targetPositionX >= 0)
					originX += (SPEED * deltaTime) / 1000;
				else
				{
					originX += ((SPEED - targetPositionX) * deltaTime) / 1000;
					
					targetPositionX = 0;
				}
			}
			else if (targetPositionX < 0)
			{
				targetPositionX += (SPEED * deltaTime) / 1000;
				
				if (targetPositionX <= 0)
					originX -= (SPEED * deltaTime) / 1000;
				else
				{
					originX -= ((SPEED - targetPositionX) * deltaTime) / 1000;
					
					targetPositionX = 0;
				}
			}
			
			if (targetPositionY > 0)
			{
				targetPositionY -= (SPEED * deltaTime) / 1000;
				
				if (targetPositionY >= 0)
					originY += (SPEED * deltaTime) / 1000;
				else
				{
					originY += ((SPEED - targetPositionY) * deltaTime) / 1000;
					
					targetPositionY = 0;
				}
			}
			else if (targetPositionY < 0)
			{
				targetPositionY += (SPEED * deltaTime) / 1000;
				
				if (targetPositionY <= 0)
					originY -= (SPEED * deltaTime) / 1000;
				else
				{
					originY -= ((SPEED - targetPositionY) * deltaTime) / 1000;
					
					targetPositionY = 0;
				}
			}*/
		}
		
		if (originX < context.tileW)
			originX = context.tileW;
		
		if (originY < context.tileH)
			originY = context.tileH;
		
		layerGround.update(originX, originY);
	}
}